package com.heima.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.common.constants.AuthStatusConfig;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojo.ApAuth;
import com.heima.model.user.pojo.ApUser;
import com.heima.user.mapper.ApAuthMapper;
import com.heima.user.service.ApAuthService;
import com.heima.user.service.ApUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional
public class ApAuthServiceImpl extends ServiceImpl<ApAuthMapper, ApAuth> implements ApAuthService {
    private final IWemediaClient iWemediaClient;

    private final ApUserService  apUserService;

    public ApAuthServiceImpl(ApUserService apUserService, IWemediaClient iWemediaClient) {
        this.apUserService = apUserService;
        this.iWemediaClient = iWemediaClient;
    }

    /**
     * 用户审核，查询列表
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult listAuth(AuthDto authDto) {
        //判断参数
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        authDto.checkParam();
        IPage page = new Page(authDto.getPage(),authDto.getSize());

        //查询条件对象
        LambdaQueryWrapper<ApAuth> queryWrapper = new LambdaQueryWrapper();

        if (authDto.getStatus()!=null){
            queryWrapper.eq(ApAuth::getStatus,authDto.getStatus());
        }

        //按照创建时间倒序查询
        queryWrapper.orderByDesc(ApAuth::getCreatedTime);

        page = page(page,queryWrapper);

        ResponseResult responseResult = new PageResponseResult(authDto.getPage(),authDto.getSize(),(int)page.getTotal());

        responseResult.setData(page.getRecords());
        responseResult.setCode(200);

        return responseResult;
    }

    /**
     * 审核失败
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto authDto) {
        //判断参数
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //驳回
        boolean update = this.update(Wrappers.<ApAuth>lambdaUpdate().eq(ApAuth::getId, authDto.getId())
                .set(ApAuth::getStatus, AuthStatusConfig.Failure)
                .set(ApAuth::getReason,authDto.getMsg())
        );
        //状态修改是否成功
        if (update){
            return ResponseResult.okResult(200,authDto.getMsg());
        }else {
            return ResponseResult.errorResult(403,"驳回失败");
        }

    }

    /**
     * 审核成功
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto authDto) {
        //判断参数
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //审核通过
        boolean update = this.update(Wrappers.<ApAuth>lambdaUpdate().eq(ApAuth::getId, authDto.getId())
                .set(ApAuth::getStatus, AuthStatusConfig.Pass)
        );
        //状态修改是否成功
        if (update){
            //根据传入的realname表的id查询userid
            LambdaQueryWrapper<ApAuth> authDtoQueryWrapper = new LambdaQueryWrapper<>();
            authDtoQueryWrapper.eq(ApAuth::getId,authDto.getId());
            ApAuth auth = this.getOne(authDtoQueryWrapper);
            Integer userId = auth.getUserId();
            //根据userid查询user表
            ApUser apUser = apUserService.getById(userId);

            //创建自媒体账号
            iWemediaClient.addWmUser(apUser);
            return ResponseResult.okResult(200,"审核通过");
        }else {
            return ResponseResult.errorResult(403,"添加失败");
        }
    }


}
